<?php
/*-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-*\
      author:   altair penaredondo
        date:   September 2, 2005
		
 description:   a simple class for displaying a page by number of records
 
       usage:   $pager  = Pager::getPagerData("numrow", "limit per page", "current page", "link");
			  	$offset = $pager->offset; 
				$limit  = $pager->limit;
				$link   = $pager->showPage;
\*-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-**-*-*/
class Pager
{
       function getPagerData($numHits, $limit, $page='1', $action, $option=false)
       {
           $numHits  = (int) $numHits;
           $limit    = ($limit) ? max((int) $limit, 1) : 30;
           $page     = ((int) $page > 0 ) ? (int) $page : "1";
           $numPages = ceil($numHits / $limit);
		   $showPage = "<div align=center style='FONT-WEIGHT: bold; 
						FONT-SIZE: 10pt; 
						COLOR: black; 
						FONT-FAMILY: Verdana;
						BACKGROUND-COLOR: #E4E4E4'>"; //#0C79B4 blue option
		   $showPage .= ($page > 1) ? "<a href='javascript: $action(". ($page-1) . ")' >Prev</a>   [  " :
		   		        "<a>Prev</a>   [  ";						
	       $showPage .= ($option) ? "<select id='pagenum' onchange='javascript: $action(this.value)'>" : "";
			
		   for ($i=1; $numPages >= $i; $i++){
				if ($option==true){				   
				 $showPage .= ($i == $page) ? "<option selected >$i</option>" : "<option>$i</option>";
				}else{
				 $showPage .= ($i == $page) ? "     <span>$i</span>     ":"     <a href='javascript: $action($i)' >$i</a>     ";
				}
		   }
		   $showPage .= ($option) ? "</select>" : "";
		   $showPage .= ($page >= $numPages) ? "  ]   <a>Next</a></div>" :
				          "  ]   <a href='javascript: $action(" . ($page+1) . ")' >Next</a></div>";
           $page = max($page, 1);
           $page = min($page, $numPages);
           $page = ($page < 1) ? 1 : $page;
           $offset = ($page - 1) * $limit;
           $ret = new stdClass;
           $ret->offset   = $offset;
           $ret->limit    = $limit;
           $ret->numPages = $numPages;
           $ret->page     = $page;
		   $ret->showPage = ($numPages <= 1) ? "" : $showPage;

           return $ret;
       }
}
?>
